package com.test.cofig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 一开始收集 转发信息
 */
public class TrafficRedrectHandlerInterceptor implements HandlerInterceptor {
    private final static Logger log = LoggerFactory.getLogger(TrafficRedrectHandlerInterceptor.class);
    public    boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                                Object handler) throws Exception {
        try{
           if(TrafficRedrectUtil.isOpen()){
               TrafficRedrectUtil.addRoute(request);
           }
        }catch (Throwable e){
            log.error("收集路由规则异常---e:{}",e);
        }
        return true;
    }



    public  void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                                 @Nullable Exception ex) throws Exception {
        TrafficRedrectUtil.cleanRoute();
    }
}
